package com.sky.controller.user;

import com.sky.entity.Dish;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 苏成博
 * @date 2023-02-11 18:52
 */
@RestController
@RequestMapping("/user/dish")
@Slf4j
@Api(tags = "用户端查询菜品信息")
public class UDishController {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private DishService dishService;

    @GetMapping("/list")
    @ApiOperation("用户端查询根据分类ID菜品信息")
    public Result<List<DishVO>> findUDishByCategoryId(Integer categoryId) {
        //redis缓存使用的key
        String key = "dish_" + categoryId;
        ValueOperations opsForValue = redisTemplate.opsForValue();
        List<DishVO> dishVOList = (List<DishVO>) opsForValue.get(key);
        if (dishVOList != null&&dishVOList.size()>0 ) {
            return Result.success(dishVOList);
        }
        dishVOList = dishService.findUDishByCategoryId(categoryId);
        //将此分类对应的菜品缓存进redis
        opsForValue.set(key, dishVOList);
        return Result.success(dishVOList);
    }
}
